package com.whmnx.shop.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.whmnx.base.bean.ShoppingBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.router.RouterFragmentPath;
import com.whmnx.shop.BR;
import com.whmnx.shop.R;
import com.whmnx.shop.databinding.FragmentShoppingBinding;
import com.whmnx.shop.ui.adapter.ShopCarAdapter;
import com.whmnx.shop.ui.viewmodel.ShoppingModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/21 0021 14:48
 * version: 1.0
 * @author Administrator
 */
@Route(path = RouterFragmentPath.Shop.PAGER_MSG)
public class ShoppingFragment extends BaseFragment<FragmentShoppingBinding, ShoppingModel> {
    private boolean isAll;

    private ShopCarAdapter shopListAdapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_shopping;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.initToolbar(this,binding);
       // initView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
       // ToastUtil.toastShortMessage(hidden + "");
        viewModel.initView();
    }

    private void initView() {
        binding.rvGoodsList.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvGoodsList.setItemAnimator(null);
        shopListAdapter = new ShopCarAdapter(getActivity());
        binding.rvGoodsList.setAdapter(shopListAdapter);

        Map<String, Object> params = new HashMap<>();
        params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
        params.put("pageIndex", 1);
        params.put("pageSize", 10);

        HttpClient.post(getActivity(), Constants.SELSHOPCART, params,new CallBack<List<ShoppingBean>>() {
            @Override
            public void onSuccess(List<ShoppingBean> shoppingBeans) {
                binding.refresh.finishRefresh(true);
                shopListAdapter.addFirstDataSet(shoppingBeans,true);
            }
        });
    }
}
